import { Component } from '@angular/core';

import { globalVariable, globalFunction, getNowFormatDate, toFormatDate } from '../../tools/common';
import { globaService } from '../../tools/globaService'; // 定义全局文件


interface Point {
  x: number; // 像素X坐标
  y: number; // 像素Y坐标
}


@Component({
  selector: 'app-test-data',
  templateUrl: './test-data.component.html',
  styleUrls: ['./test-data.component.less']
})
export class TestDataComponent {

  constructor(
    public globaService: globaService
  ) { }

  nowdate: string = "";

  // ngOnInit(): void {
  //   console.log(333);
  //   // console.log(globalVariable); // 输出 'Hello, world!'
  //   // globalFunction(); // 调用全局函数
  //   this.nowdate = getNowFormatDate(); // 获取最新时间
  //   console.log(this.nowdate);

  //   console.log(this.globaService.getNowFormatDate());


  // }


  getNowDate() {
    this.nowdate = getNowFormatDate();
  }

  toDate() {
    // let date = new Date('Wed Apr 03 2024 11:50:00 GMT+0800 (中国标准时间)')
    // console.log(date.toISOString(), 'date');

    // let a = toFormatDate('Wed Apr 03 2024 11:50:00 GMT+0800 (中国标准时间)')
    // console.log(a, 'Wed Apr 03 2024 11:50:00 GMT+0800 (中国标准时间)');

  }


  animationBtn: boolean = false;
  handlerAnimation() {
    this.animationBtn = !this.animationBtn;
    console.log(this.animationBtn, 'this.animationBtn');

  }



  firstPoint: Point | null = null;
  secondPoint: Point | null = null;
  currentDistance = 0; // 厘米距离
  showDistance = false;
  private pixelsPerCm = 0; // 每厘米像素数（根据屏幕DPI计算）

  ngOnInit() {
    // 初始化时计算屏幕每厘米像素数（1英寸=2.54厘米，DPI为每英寸像素）
    const dpi = this.getScreenDPI();
    this.pixelsPerCm = dpi / 2.54; // 转换为每厘米像素数
  }

  // 获取屏幕DPI（简化版，实际可根据设备调整）
  private getScreenDPI(): number {
    // 大多数显示器DPI在96-120之间，这里取100作为默认值
    // 精确值可通过window.screen或canvas测量获取
    return 100;
  }

  // 点击第一个点
  onFirstPointClick(event: MouseEvent): void {
    if (!this.firstPoint) {
      this.firstPoint = {
        x: event.offsetX,
        y: event.offsetY
      };
      this.showDistance = true;
    }
  }

  // 鼠标移动时更新临时第二个点
  onMouseMove(event: MouseEvent): void {
    if (this.firstPoint && !this.secondPoint) {
      const tempPoint = {
        x: event.offsetX,
        y: event.offsetY
      };
      // 计算像素距离并转换为厘米
      const pixelDistance = this.calculatePixelDistance(this.firstPoint, tempPoint);
      this.currentDistance = pixelDistance / this.pixelsPerCm;
    }
  }

  // 确认第二个点
  onSecondPointClick(event: MouseEvent): void {
    if (this.firstPoint && !this.secondPoint) {
      this.secondPoint = {
        x: event.offsetX,
        y: event.offsetY
      };
      // 计算最终距离（厘米）
      const pixelDistance = this.calculatePixelDistance(this.firstPoint, this.secondPoint);
      this.currentDistance = pixelDistance / this.pixelsPerCm;
    }
  }

  // 计算两点像素距离
  private calculatePixelDistance(p1: Point, p2: Point): number {
    const dx = p2.x - p1.x;
    const dy = p2.y - p1.y;
    return Math.sqrt(dx * dx + dy * dy);
  }

  // 重置测量
  reset(): void {
    this.firstPoint = null;
    this.secondPoint = null;
    this.currentDistance = 0;
    this.showDistance = false;
  }

  ngOnDestroy() {
    console.log(444);
  }
}
